/**
 * <p>Title: NTrip</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author David So
 * @version 1.0
 */

import java.io.*;
import java.net.*;

public class NTrip {

  private static final String AGENT_STRING = "NTRIP ProjectGPSse/0.1";

  private static final String HOST_NAME = "www.euref-ip.net";
 // private static final String HOST_NAME = "129.94.167.180";
  private static final int HTTP_PORT = 2101;

  private static final String MOUNT_POINT = "SYDN0";
  private static final String USER_NAME = "user96";
  private static final String USER_PASS = "hfjfkp";

  Socket clientSocket;
  DataOutputStream outToServer;
  BufferedReader inFromServer;
  String encryptedAccess;

  public NTrip() {
  }

  public void getSourceTable() {
    try {

      // Create TCP/IP socket
      clientSocket = new Socket(HOST_NAME, HTTP_PORT);

      // Create output stream to webserver
      outToServer = new DataOutputStream(clientSocket.
                                         getOutputStream());

      // Create input stream to webserver
      inFromServer = new BufferedReader(new InputStreamReader(
          clientSocket.getInputStream()));

      // Encode username and password with Base64
//   encryptedAccess = Base64.encode(USER_NAME + USER_PASS);
      encryptedAccess = "dXNlcjk2OmhmamZrcA==";

      // Request data from webserver
      outToServer.writeBytes("GET /" + " HTTP/1.0\r\n"
                             + "User-Agent: NTRIP NtripLinuxClient/1.9\r\n"
                             + "Authorization: Basic " + encryptedAccess +
                             "\r\n\r\n");

      // Read first line from webserver's reply
      String reply = inFromServer.readLine();

      int i = 0;
      // Recurse until no lines are left (Never happens - infinite loop)
      while (reply != null) {
        if(reply.startsWith("STR;")){
          i++;
          System.out.println(i + " " + reply);
        }

        // Next line
        reply = inFromServer.readLine();
      }

      outToServer.close();
      inFromServer.close();
      clientSocket.close();

    }
    catch (Exception e) {

    }

  }

  public void streamData(OutputStream serialOut) {

    try {

      // Create TCP/IP socket
      clientSocket = new Socket(HOST_NAME, HTTP_PORT);

      // Create output stream to webserver
      outToServer = new DataOutputStream(clientSocket.
                                         getOutputStream());

      // Create input stream to webserver
      inFromServer = new BufferedReader(new InputStreamReader(
          clientSocket.getInputStream()));

      // Encode username and password with Base64
   //   encryptedAccess = Base64.encode(USER_NAME + USER_PASS);
      encryptedAccess = "dXNlcjk2OmhmamZrcA==";

      // Request data from webserver
      outToServer.writeBytes("GET /" + MOUNT_POINT + " HTTP/1.0\r\n"
                             + "User-Agent: NTRIP NtripLinuxClient/1.9\r\n"
                             + "Authorization: Basic " + encryptedAccess + "\r\n\r\n");


      // Read first line from webserver's reply
      String reply = inFromServer.readLine();

      // Recurse until no lines are left (Never happens - infinite loop)
      while (reply != null) {
        System.out.println(reply);

        // Write line to given output stream (eg. serial port)
        //serialOut.write((reply + "\r\n").getBytes());

        // Next line
        reply = inFromServer.readLine();
      }

      serialOut.close();
      outToServer.close();
      inFromServer.close();
      clientSocket.close();

    }
    catch (Exception e) {
      System.out.println("Exception @ getting NTrip data stream");
    }
    return ;
  }

}
